package pl.radiomaryja.softcognito;

import java.io.IOException;
import java.util.List;

import org.softcognito.webcontent.radiomaryja.CategoryDTO;
import org.softcognito.webcontent.radiomaryja.CategoryDTOImpl;
import org.softcognito.webcontent.radiomaryja.ContentImpl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;


public class RMSecondLevel extends Activity
 {

    private ListView listView1;
    private List<CategoryDTO> cl;
    private AdapterCategories adapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ContentImpl c = new ContentImpl();
        try {
			cl = c.getCategories("http://www.radiomaryja.pl");
		} catch (IOException e) {
			Log.i("CantGetContent", e.getMessage());
		}
        
        adapter = new AdapterCategories(this,
                R.layout.listview_category_row, cl);
       
        listView1 = (ListView)findViewById(R.id.listView1);
        
        listView1.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {

                if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
                {
/*                    if(flag_loading == false)
                    {
                        flag_loading = true;
                        additems();
                    }*/
                	cl.add(new CategoryDTOImpl("ddd", "asdasdsada"+totalItemCount));
                	adapter.notifyDataSetChanged();
           //     	Log.i("visibleItemCount", "" + firstVisibleItem + " "+ visibleItemCount + " " + totalItemCount );
                }
			}

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				
			}
        });
        
        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        listView1.addHeaderView(header);
       
        listView1.setAdapter(adapter);
        
        listView1.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View arg1, int position,
					long arg3)
            {
        		Intent myIntents = new Intent(arg1.getContext(), ArticlesListView.class);
        		myIntents.putExtra("link", cl.get(position-1).getLink());
        		startActivity(myIntents);
            }

        });
        
    }


}
